package com.defense;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class MangerInterface {
    private JFrame jf = new JFrame("课堂随机点名系统");
    private final int width = 800;
    private final int height = 600;
    private JMenuBar jmb = new JMenuBar();
    private JMenu file = new JMenu("文件");
    private JMenu clas = new JMenu("班级管理");
    private JMenu group = new JMenu("小组管理");
    private JMenu stu = new JMenu("学生管理");
    private JMenu classroom = new JMenu("课堂管理");
    private JMenuItem newclass = new JMenuItem("新建班级");
    private JMenuItem classlist = new JMenuItem("班级列表");
    private JMenuItem newgroup = new JMenuItem("新建小组");
    private JMenuItem grouplist = new JMenuItem("小组列表");
    private JMenuItem newstudent = new JMenuItem("新建学生");
    private JMenuItem studentlist = new JMenuItem("学生列表");
    private JMenuItem ranstu = new JMenuItem("随机学生");
    private JMenuItem rangroup = new JMenuItem("随机小组");

    // 提取初始化逻辑为一个方法，而不是在main中直接执行
    public void init() {
        jf.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - width) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - height) / 2,
                width, height);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口默认关闭操作

        // 组装顶部菜单
        clas.add(newclass);
        clas.add(classlist);
        group.add(newgroup);
        group.add(grouplist);
        stu.add(newstudent);
        stu.add(studentlist);
        classroom.add(ranstu);
        classroom.add(rangroup);
        jmb.add(file);
        jmb.add(clas);
        jmb.add(group);
        jmb.add(stu);
        jmb.add(classroom);
        jf.setJMenuBar(jmb);

        // 在页面中间添加显示文字的标签
        JLabel middleLabel = new JLabel();
        updateMiddleLabelText(middleLabel);
        jf.add(middleLabel, BorderLayout.CENTER);

        jf.setVisible(true);

        // 新建班级添加点击事件
        newclass.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NewClass newClassPage = new NewClass();
                newClassPage.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });
        // 班级列表添加点击事件
        classlist.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ClassList classListPage = new ClassList();
                classListPage.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });
        // 新建小组添加点击事件
        newgroup.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NewGroup newGroupPage = new NewGroup();
                newGroupPage.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });
        // 小组列表添加点击事件
        grouplist.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GroupList groupListPage = new GroupList();
                groupListPage.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });
        // 新建学生添加点击事件
        newstudent.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NewStudent newStudentPage = new NewStudent();
                newStudentPage.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });
        // 学生列表添加点击事件
        studentlist.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StuList studentListPage = new StuList();
                studentListPage.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });
        // 随机学生添加点击事件
        ranstu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RanStu randomStuPage = new RanStu();
                randomStuPage.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });
        // 随机小组添加点击事件
        rangroup.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RanGroup randomGroupPage = new RanGroup();
                randomGroupPage.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });
    }

    // 添加一个方法用于显示界面，方便在登录成功后调用
    public void showInterface() {
        init();
    }

    public static void main(String[] args) {
        MangerInterface mangerInterface = new MangerInterface();
        mangerInterface.showInterface();
    }

    // 方法用于更新中间标签的文本内容
    private void updateMiddleLabelText(JLabel label) {
        File classFile = new File("class.txt");
        if (classFile.exists() && classFile.length() > 0) {
            try (BufferedReader br = new BufferedReader(new FileReader(classFile))) {
                String line;
                String lastLine = "";
                while ((line = br.readLine())!= null) {
                    lastLine = line;
                }
                label.setText("当前班级: " + lastLine);
                label.setForeground(Color.RED);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            label.setText("请选择班级");
            label.setForeground(Color.RED);
        }
    }
}
